﻿using AnesSystem.Controls;
using AnesSystem.Models;
using AnesSystem.Models.Model;
using AnesSystem.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AnesSystem.ViewModels.LaborAnalgesia
{
    public class LaborAnalgesiaProcessViewModel : BaseViewModel<LaborAnalgesiaProcessViewModel>
    {
        #region Variables
        private List<PatientVitalSignsModel> _allLaborVitalList;
        private LaborRealTimeItemCtrlViewModel _model;



        #endregion


        #region Constructor
        public LaborAnalgesiaProcessViewModel()
        {
            _allLaborVitalList = new List<PatientVitalSignsModel>();
        }

        
        #endregion


        #region Properties

        public List<PatientVitalSignsModel> AllLaborVitalList
        {
            get
            {
                RefreshVitals();
                return _allLaborVitalList;
            }

            set
            {
                _allLaborVitalList = value;
            }
        }

        public LaborRealTimeItemCtrlViewModel Model
        {
            get
            {
                return _model;
            }

            set
            {
                _model = value;
            }
        }


        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods


        #endregion


        #region Public/ProtectedMethods
        public void StartLabor()
        {
            DateTime date = new DateTime();
            CusDatePickerWin win = new CusDatePickerWin();
            win.SelectedDateTime = DateTime.Now;
            win.ShowDialog();
            if (win.Result)
            {
                date = win.SelectedDateTime;
            }
            if (!date.Equals(new DateTime()))
            {

                date = date.AddSeconds(-date.Second).AddMilliseconds(-date.Millisecond);
                DataManager.Instance.CurrentPatientLink.Status.InLaborTime = date;
                date = date.AddMinutes(-date.Minute % 5);
                DataManager.Instance.CurrentPatientLink.Status.LaborRecordStartTime = date;
                InitVitalConfig();
                SaveOper2DB();
                RefreshLaborData();
                AddEvent(DataManager.Instance.CurrentPatientLink.Status.InLaborTime);
                GotoLaborCtrl();
            }
        }

        private void AddEvent(DateTime date)
        {
            PatientLaborEventModel ple = new PatientLaborEventModel();
            ple.Name = "分娩镇痛开始";
            ple.Code = "分娩镇痛开始";
            ple.PatientID = PatientLink.Id.ToString();
            ple.BeginTime = date;
            ple.NotEdit = true;
            PatientLaborEventService.Instance.AddUpdate(ple);
        }

        private void InitVitalConfig()
        {
            if (DataManager.Instance.CurrentPatientLink.VitalSignShow.LaborVitalSign==null|| DataManager.Instance.CurrentPatientLink.VitalSignShow.LaborVitalSign.Count<=0)
            {
                DataManager.Instance.CurrentPatientLink.VitalSignShow.LaborVitalSign.Add(DataManager.Instance.Code2signs["param_spo2"]) ;
                DataManager.Instance.CurrentPatientLink.VitalSignShow.LaborVitalSign.Add(DataManager.Instance.Code2signs["param_vas"]);
                DataManager.Instance.CurrentPatientLink.VitalSignShow.LaborVitalSign.Add(DataManager.Instance.Code2signs["param_bromage"]);
                DataManager.Instance.CurrentPatientLink.VitalSignShow.LaborVitalSign.Add(DataManager.Instance.Code2signs["param_T"]);
                DataManager.Instance.CurrentPatientLink.VitalSignShow.LaborVitalSign.Add(DataManager.Instance.Code2signs["param_taixin"]);
                DataManager.Instance.CurrentPatientLink.VitalSignShow.LaborVitalSign.Add(DataManager.Instance.Code2signs["param_HR"]);
                DataManager.Instance.CurrentPatientLink.VitalSignShow.LaborVitalSign.Add(DataManager.Instance.Code2signs["param_nibp_s"]);
                DataManager.Instance.CurrentPatientLink.VitalSignShow.LaborVitalSign.Add(DataManager.Instance.Code2signs["param_nibp_d"]);
            }
        }

        private void RefreshLaborData()
        {
            LaborVitalData laborData = DataManager.Instance.CurrentPatientLink.LaborAnalgesia.LaborVitalData;
            if (laborData == null)
            {
                laborData = new LaborVitalData();
                DataManager.Instance.CurrentPatientLink.LaborAnalgesia.LaborVitalData = laborData;
                CreatVitalSigns();
            }
            else
            {
                RefreshTimeData();
            }
            SaveOper2DB();
        }

        public void RefreshTimeData()
        {
            List<PatientVitalSignsModel> timeList = DataManager.Instance.CurrentPatientLink.LaborAnalgesia.LaborVitalData.TimeList;
            timeList.Clear();
            for (int i = 0; i <= 30; i++)
            {
                timeList.Add(CreatPatientVitals("time", i));
            }
            SaveOper2DB();
            GotoLaborCtrl();
        }

        private void CreatVitalSigns()
        {
            List<PatientVitalSignsModel> timeList = new List<PatientVitalSignsModel>();
            List<PatientVitalSignsModel> spo2List = new List<PatientVitalSignsModel>();
            List<PatientVitalSignsModel> vasList = new List<PatientVitalSignsModel>();
            List<PatientVitalSignsModel> bromageList = new List<PatientVitalSignsModel>();
            List<PatientVitalSignsModel> twList = new List<PatientVitalSignsModel>();
            List<PatientVitalSignsModel> taixinList = new List<PatientVitalSignsModel>();
            List<PatientVitalSignsModel> hrList = new List<PatientVitalSignsModel>();
            List<PatientVitalSignsModel> bpsList = new List<PatientVitalSignsModel>();
            List<PatientVitalSignsModel> bpdList = new List<PatientVitalSignsModel>();

            for (int i = 0; i <= 30; i++)
            {
                timeList.Add(CreatPatientVitals("time", i));
                spo2List.Add(CreatPatientVitals("param_spo2", i));
                vasList.Add(CreatPatientVitals("param_vas", i));
                bromageList.Add(CreatPatientVitals("param_bromage", i));
                twList.Add(CreatPatientVitals("param_T", i));
                taixinList.Add(CreatPatientVitals("param_taixin", i));
                hrList.Add(CreatPatientVitals("param_HR", i));
                bpsList.Add(CreatPatientVitals("param_nibp_s", i));
                bpdList.Add(CreatPatientVitals("param_nibp_d", i));
            }
            PatientLink.LaborAnalgesia.LaborVitalData.TimeList = timeList;
            PatientLink.LaborAnalgesia.LaborVitalData.Spo2List = spo2List;
            PatientLink.LaborAnalgesia.LaborVitalData.VasList = vasList;
            PatientLink.LaborAnalgesia.LaborVitalData.BromageList = bromageList;
            PatientLink.LaborAnalgesia.LaborVitalData.TwList = twList;
            PatientLink.LaborAnalgesia.LaborVitalData.TaixinList = taixinList;
            PatientLink.LaborAnalgesia.LaborVitalData.HrList = hrList;
            PatientLink.LaborAnalgesia.LaborVitalData.BpsList = bpsList;
            PatientLink.LaborAnalgesia.LaborVitalData.BpdList = bpdList;

        }

        private PatientVitalSignsModel CreatPatientVitals(string code, int index)
        {
            DateTime startTime = DataManager.Instance.CurrentPatientLink.Status.LaborRecordStartTime;
            DateTime endTime = startTime.AddHours(1);
            PatientVitalSignsModel pvm = new PatientVitalSignsModel();
            pvm.LaborIndex = index;
            pvm.PatientID = DataManager.Instance.CurrentPatientLink.Id.ToString();
            if ("time".Equals(code))
            {
                pvm.Code = "TIME_CODE";
                if (index <= 12)
                {

                    pvm.CaptureTime = startTime.AddMinutes(index * 5);

                }
                else
                {
                    pvm.CaptureTime = endTime.AddMinutes((index - 12) * 20);
                }
            }
            else
            {
                ConfigVitalSignsModel cvs = null;
                if (DataManager.Instance.Code2signs.ContainsKey(code))
                {
                    cvs = DataManager.Instance.Code2signs[code];
                }
                if (cvs != null)
                {

                    pvm.Name = cvs.name;
                    pvm.SimpleName = cvs.simpleName;
                    pvm.Code = cvs.code;
                    pvm.ModValue = "";
                    pvm.IsAorP = Constants.ISLABOR;
                }
            }
            return pvm;
        }

        private void Pvm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            PatientVitalSignsModel pvm = sender as PatientVitalSignsModel;
            if (!("TIME_CODE").Equals(pvm.Code))
            {
                Model.RefreshByGroup(pvm.Code);
            }
           
            SaveOper2DB();
        }

        public void GotoLaborCtrl()
        {
            AddLister();
            DataManager.Instance.IsInAorP = Constants.ISLABOR;
            string fullName = "AnesSystem.Controls." + "CommonTables.CQ.CQFY.LaborAnalgesiaCtrl";
            Type type = Type.GetType(fullName);
            Object obj = obj = Activator.CreateInstance(type);
            DataManager.Instance.RightContentCtrlViewModel.CurrentRightContent = obj as FrameworkElement;
        }

        private void AddLister()
        {
            foreach (var item in AllLaborVitalList)
            {
                item.PropertyChanged += Pvm_PropertyChanged;
            }
        }

        private void SaveOper2DB()
        {
            PatientLinkService.Instance.AddUpdate(DataManager.Instance.CurrentPatientLink);
        }

        public void RefreshData()
        {
            GotoLaborCtrl();
        }

        private void RefreshVitals()
        {
            _allLaborVitalList.Clear();
            _allLaborVitalList.AddRange(PatientLink.LaborAnalgesia.LaborVitalData.TimeList);
            _allLaborVitalList.AddRange(PatientLink.LaborAnalgesia.LaborVitalData.Spo2List);
            _allLaborVitalList.AddRange(PatientLink.LaborAnalgesia.LaborVitalData.VasList);
            _allLaborVitalList.AddRange(PatientLink.LaborAnalgesia.LaborVitalData.BromageList);
            _allLaborVitalList.AddRange(PatientLink.LaborAnalgesia.LaborVitalData.TwList);
            _allLaborVitalList.AddRange(PatientLink.LaborAnalgesia.LaborVitalData.TaixinList);
            _allLaborVitalList.AddRange(PatientLink.LaborAnalgesia.LaborVitalData.HrList);
            _allLaborVitalList.AddRange(PatientLink.LaborAnalgesia.LaborVitalData.BpsList);
            _allLaborVitalList.AddRange(PatientLink.LaborAnalgesia.LaborVitalData.BpdList);
           
        }

        #endregion


        #region EventHandlers


        #endregion
    }
}
